package com.example.educational.service.impl;


import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.educational.entity.User;
import com.example.educational.mapper.UserMapper;
import com.example.educational.service.InnerAppService;
import com.example.educational.service.ReadPeopleService;
import com.example.educational.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author MySoul
 * @description 针对表【edu_user】的数据库操作Service实现
 * @createDate 2023-05-18 17:23:18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {

    @Autowired
    UserMapper userMapper;
    @Autowired
    ReadPeopleService readPeopleService;
    @Autowired
    InnerAppService innerAppService;

    @Override
    public Boolean recordReadUser(String userId, String taskId) {

        Boolean result = Boolean.FALSE;
        try {
            int i = userMapper.insertReadPeople(userId, taskId);
            if (i > 0) {
                result = Boolean.TRUE;
            }
        } catch (DataIntegrityViolationException e) {
            // 该用户重复读取
            int i = readPeopleService.recordReadCount(userId, taskId);
            if (i > 0) {
                result = Boolean.TRUE;
            }
        }

        return result;
    }

    @Override
    public Boolean createUser(String accessToken, String userId) {

        // 1.获取用户信息
        User user = innerAppService.getUserDetailByUserId(accessToken, userId);
        // 插入用户
        int result = userMapper.insert(user);
        return result > 0 ? Boolean.TRUE : Boolean.FALSE;

    }

    @Override
    public User checkUserIdentify(String userId) {
        User user;

        user = userMapper.selectById(userId);

        return user;
    }

    @Override
    public ArrayList<String> changeIdFromName(String userNameList) {


        List<String> list = JSONUtil.toList(userNameList, String.class);

        ArrayList<String> arrayList = userMapper.selectIdsByNames(list);

        return arrayList;
    }

    @Override
    public ArrayList<User> getUsersRead(String taskId, String name) {
        ArrayList<User> userArrayList;

        if (ObjectUtil.isNull(name)) {
            name = "";
        }
        userArrayList = userMapper.selectUsersReadByTaskId(taskId, name);


        return userArrayList;
    }

    @Override
    public ArrayList<User> getUsersUnRead(String taskId, String name) {
        ArrayList<User> userArrayList;

        if (ObjectUtil.isNull(name)) {
            name = "";
        }
        userArrayList = userMapper.selectUsersUnReadByTaskId(taskId, name);

        return userArrayList;
    }

    @Override
    public Boolean deleteUser(String userId) {
        int i;
        // 删除用户
        i = userMapper.deleteById(new User(userId));
        return i > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public Boolean updateUser(String accessToken, String userId) {
        int i = 0;
        // 1.获取用户信息
        User user = innerAppService.getUserDetailByUserId(accessToken, userId);
        // 2.更新用户信息
        i = userMapper.updateById(user);
        return i > 0 ? Boolean.TRUE : Boolean.FALSE;
    }

    @Override
    public Boolean updateUser(String accessToken, String oldUserId, String newUserId) {
        int i = 0;
        // 1.获取用户信息
        User newUser = innerAppService.getUserDetailByUserId(accessToken, newUserId);
        User oldUser = userMapper.selectById(oldUserId);
        // 2.更新用户信息
        LambdaQueryWrapper<User> updateWrapper = new LambdaQueryWrapper<>();
        updateWrapper.eq(User::getId, oldUser.getId());
        i = userMapper.update(newUser, updateWrapper);
        return i > 0 ? Boolean.TRUE : Boolean.FALSE;
    }
}




